package cn.fl.risk.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.risk.api.IRskPrdSignLogApiService;
import cn.fl.risk.condition.RskPrdSignLogQueryCondition;
import cn.fl.risk.dto.RskPrdSignLogDTO;
import cn.fl.risk.service.IRskPrdSignLogService;
import cn.fl.risk.transform.RskPrdSignLogVOTransform;
import cn.fl.risk.vo.RskPrdSignLogVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description: 设备风险标记记录表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class RskPrdSignLogServiceImpl implements IRskPrdSignLogService {

    /**
     * 日志
     */
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(RskPrdSignLogServiceImpl.class);

    /**
     * 设备风险标记记录表 暴露服务
     */
    @Autowired
    private IRskPrdSignLogApiService rskPrdSignLogApiService;
    /**
     * 数据字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

    @Override
    public RskPrdSignLogVO selectById(Long id) throws BaseException {
        if (StringUtils.isEmpty(id)) return null;
        RskPrdSignLogDTO dto = this.rskPrdSignLogApiService.selectRskPrdSignLogById(id);
        RskPrdSignLogVO vo = RskPrdSignLogVOTransform.toVO(dto);
        setDictDataSingle(vo);
        return vo;
    }

    @Override
    public Long add(RskPrdSignLogVO entity) throws BaseException {
        if (null == entity) return null;
        return this.rskPrdSignLogApiService.addRskPrdSignLog(RskPrdSignLogVOTransform.toDTO(entity));
    }

    @Override
    public boolean removeByIds(List<Long> ids) throws BaseException {
        if (CollectionUtils.isEmpty(ids)) return false;
        return this.rskPrdSignLogApiService.removeRskPrdSignLogByIds(ids, CurrentThreadContext.getCurrentUserId());
    }

    @Override
    public boolean modify(RskPrdSignLogVO entity) throws BaseException {
        if (null == entity) return false;
        return this.rskPrdSignLogApiService.modifyRskPrdSignLog(RskPrdSignLogVOTransform.toDTO(entity));
    }

    @Override
    public Pager searchListPage(Pager page) throws BaseException {
        RskPrdSignLogQueryCondition condition = null;
        if (null == page) {
            page = new Pager();
            page.setCondition(new RskPrdSignLogQueryCondition());
        }
        condition = (RskPrdSignLogQueryCondition) page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
        page = this.rskPrdSignLogApiService.searchRskPrdSignLogListPage(page);
        if (CollectionUtils.isNotEmpty(page.getResultList())) {
            List<RskPrdSignLogVO> voList = RskPrdSignLogVOTransform.toVOList((List<RskPrdSignLogDTO>) page.getResultList());
            this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
    }

    @Override
    public List<RskPrdSignLogVO> searchList(RskPrdSignLogQueryCondition condition) throws BaseException {
        if (null == condition) condition = new RskPrdSignLogQueryCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
        List<RskPrdSignLogVO> voList = RskPrdSignLogVOTransform.toVOList(this.rskPrdSignLogApiService.searchRskPrdSignLogList(condition));
        this.setDictData(voList);
        return voList;
    }

    /**
     * 添加数据字典名称
     *
     * @param voList
     */
    public void setDictData(List<RskPrdSignLogVO> voList) {
        if (CollectionUtils.isEmpty(voList)) return;
        /**
         * 设备风险标记原因
         */
        Map<String, String> resCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), DictionaryConstant.PRD_RISK_SINGLOG);
        /**
         * 风险标记类型
         */
        Map<String, String> resTypeMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), DictionaryConstant.CODE_GRP_NM_SIGN_RES_TYPE);
		for(RskPrdSignLogVO vo:voList){
            if (resCdMap != null && resCdMap.containsKey(vo.getSignResCd())) {
                vo.setSignResCdNam(resCdMap.get(vo.getSignResCd()));
            }
            if (resTypeMap != null && resTypeMap.containsKey(vo.getSignResType())) {
                vo.setSignResTypeNm(resTypeMap.get(vo.getSignResType()));
            }
        }
    }

    /**
     * 添加数据字典名称
     *
     * @param vo
     */
    public void setDictDataSingle(RskPrdSignLogVO vo) {
        if (vo == null) return;
        /**
         * 设备风险标记原因
         */
        Map<String, String> resCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), DictionaryConstant.PRD_RISK_SINGLOG);
        /**
         * 风险标记类型
         */
        Map<String, String> resTypeMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), DictionaryConstant.CODE_GRP_NM_SIGN_RES_TYPE);
        if (resCdMap != null && resCdMap.containsKey(vo.getSignResCd())) {
            vo.setSignResCdNam(resCdMap.get(vo.getSignResCd()));
        }
        if (resTypeMap != null && resTypeMap.containsKey(vo.getSignResType())) {
            vo.setSignResTypeNm(resTypeMap.get(vo.getSignResType()));
        }
    }

    /**
     * 查询数据字典
     *
     * @param groupCd 组代码
     * @return List<KeyValueVO>
     */
    public List<KeyValueVO> searchCdList(String groupCd) {
        return this.sysDictionaryApiService.getKvList(groupCd, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<RskPrdSignLogVO> selectChangVin(String vin) throws BaseException {
        if (StringUtils.isEmpty(vin)) return null;
        List<RskPrdSignLogDTO> dto = this.rskPrdSignLogApiService.selectRskPrdSignLogByVin(vin);
        List<RskPrdSignLogVO> vo = RskPrdSignLogVOTransform.toVOList((List<RskPrdSignLogDTO>) dto);
        return vo;
    }

    @Override
    public RskPrdSignLogVO selectPrjPrdDtlId(String vin) throws BaseException {
        if (StringUtils.isEmpty(vin)) return null;
        RskPrdSignLogDTO dto = this.rskPrdSignLogApiService.selectPrjPrdDtlId(vin);
        RskPrdSignLogVO vo = RskPrdSignLogVOTransform.toVO(dto);
        return vo;
    }

    @Override
    public boolean removeById(Long id) {
        return this.rskPrdSignLogApiService.removeRskPrdSignLogById(id);
    }

    @Override
    public Pager searchVINListPage(Pager page) throws BaseException {
        RskPrdSignLogQueryCondition condition = null;
        if (null == page) {
            page = new Pager();
            page.setCondition(new RskPrdSignLogQueryCondition());
        }
        condition = (RskPrdSignLogQueryCondition) page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
        page = this.rskPrdSignLogApiService.searchVINListPage(page);
        if (CollectionUtils.isNotEmpty(page.getResultList())) {
            List<RskPrdSignLogVO> voList = RskPrdSignLogVOTransform.toVOList((List<RskPrdSignLogDTO>) page.getResultList());
            //this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
    }

    @Override
    public RskPrdSignLogVO selectChangVinPaySchNo(String paySchNo) {
        if (StringUtils.isEmpty(paySchNo)) return null;
        RskPrdSignLogDTO dto = this.rskPrdSignLogApiService.selectChangVinPaySchNo(paySchNo);
        RskPrdSignLogVO vo = RskPrdSignLogVOTransform.toVO(dto);
        return vo;
    }

    @Override
    public RskPrdSignLogVO selectPrjPrdDtlIdVin(String vin, String paySchNo) {
        if (StringUtils.isEmpty(vin)) return null;
        if (StringUtils.isEmpty(paySchNo)) return null;
        RskPrdSignLogDTO dto = this.rskPrdSignLogApiService.selectPrjPrdDtlIdVin(vin,paySchNo);
        RskPrdSignLogVO vo = RskPrdSignLogVOTransform.toVO(dto);
        return vo;
    }


}
